package cn.walls1717.mall.modules.pms.controller;


import cn.walls1717.mall.common.api.CommonPage;
import cn.walls1717.mall.common.api.CommonResult;
import cn.walls1717.mall.common.util.ComResultUtil;
import cn.walls1717.mall.modules.pms.dto.ProductConditionDTO;
import cn.walls1717.mall.modules.pms.dto.ProductSaveParamDTO;
import cn.walls1717.mall.modules.pms.dto.ProductUpdateInitDTO;
import cn.walls1717.mall.modules.pms.model.PmsProduct;
import cn.walls1717.mall.modules.pms.service.PmsProductService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author walls1717
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/products")
public class PmsProductController {

    private PmsProduct product;

    private final PmsProductService productService;
    @Autowired
    public PmsProductController(PmsProductService productService) {
        this.productService = productService;
    }

    @ApiOperation("商品分页列表")
    @GetMapping()
    public CommonResult<CommonPage<PmsProduct>> pageList(ProductConditionDTO condition) {
        Page<PmsProduct> list = productService.pageList(condition);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("商品添加")
    @PostMapping()
    public CommonResult<?> createProduct(@RequestBody ProductSaveParamDTO productSaveParamDTO) {
        boolean result = productService.createProduct(productSaveParamDTO);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品编辑")
    @PutMapping("{id}")
    public CommonResult<?> updateProduct(@RequestBody @Valid ProductSaveParamDTO productSaveParamDTO) {
        Boolean result = productService.updateProduct(productSaveParamDTO);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品数据删除")
    @PutMapping("deleteStatus")
    public CommonResult<?> delete(@RequestParam("ids") List<Long> ids) {
        boolean result = productService.removeByIds(ids);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("获取编辑页面预填充信息")
    @GetMapping("{id}")
    public CommonResult<ProductUpdateInitDTO> getOneProduct(@PathVariable("id") Long id) {
        ProductUpdateInitDTO productUpdateInitDTO = productService.getOneProduct(id);
        return CommonResult.success(productUpdateInitDTO);
    }

    @ApiOperation("商品上架状态设置")
    @PutMapping("publishStatus")
    public CommonResult<?> updatePublishStatus(@RequestParam("ids") List<Long> ids, @RequestParam("publishStatus") Integer publishStatus) {
        product = new PmsProduct();
        product.setPublishStatus(publishStatus);
        boolean result = productService.updateStatus(ids, product);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("商品新品状态设置")
    @PutMapping("newStatus")
    public CommonResult<Object> updateNewStatus(@RequestParam("ids") List<Long> ids, @RequestParam("newStatus") Integer newStatus) {
        product = new PmsProduct();
        product.setNewStatus(newStatus);
        boolean result = productService.updateStatus(ids, product);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("商品新品状态设置")
    @PutMapping("recommendStatus")
    public CommonResult<Object> updateRecommendStatus(@RequestParam("ids") List<Long> ids, @RequestParam("recommendStatus") Integer recommendStatus) {
        product = new PmsProduct();
        product.setRecommandStatus(recommendStatus);
        boolean result = productService.updateStatus(ids, product);
        if (result) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

}

